<template>
  <div>
    <PageTools>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="商品类型搜索">
          <el-input
            v-model="formInline.className"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="Search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </PageTools>
    <el-card class="main">
      <el-button
        style="margin-bottom: 10px"
        type="primary"
        size="small"
        icon="el-icon-circle-plus-outline"
        @click="addForm"
      >新建</el-button>
      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%"
        :header-cell-style="{
          fontWeight: 'normal',
          background: '#f3f6fb',
        }"
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column label="商品类型名称" prop="className" />
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="small"
              @click="detailsFn(row)"
            >修改</el-button>
            <el-button
              type="text"
              size="small"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row v-if="totalPage!==1" type="flex" justify="space-between" align="middle">
        <el-col :span="19">
          <p class="paginationHeader">
            {{
              `共${totalCount}条记录 第${page.pageIndex}/${totalPage}页`
            }}
          </p>
        </el-col>
        <el-col :span="5">
          <el-pagination
            background
            :current-page="page.pageIndex"
            :page-size="page.pageSize"
            :total="totalCount"
            layout="prev, next"
            prev-text="上一页"
            next-text="下一页"
            @current-change="changePage"
          />
        </el-col>
      </el-row>
    </el-card>
    <!-- 新增商品类型 -->
    <el-dialog
      class="dialog"
      title="新增商品类型"
      :visible="dialogSku"
      width="45%"
      @close="closeDialog"
      @open="openDialog"
    >
      <el-form
        ref="addSkuRef"
        label-width="120px"
        :model="addSkuForm"
        :rules="addSkuFormRules"
      >
        <el-form-item label="商品类型名称:" prop="className">
          <el-input
            v-model="addSkuForm.className"
            type="text"
            placeholder="请输入"
            maxlength="10"
            show-word-limit
            style="width: 80%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancel" @click="closeDialog">取 消</el-button>
        <el-button
          type="primary"
          class="sure"
          @click="skuSubmit"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { skuClassList, AddskuClass, EditskuClass } from '@/api/sku'
export default {
  data() {
    return {
      formInline: { className: '' },
      loading: false,
      list: [],
      page: {
        pageIndex: 1,
        pageSize: 10
      },
      totalCount: 0,
      totalPage: 0,
      addSkuForm: {
        className: ''
      },
      addSkuFormRules: {
        className: [{ required: true, message: '请输入', trigger: 'change' }]
      },
      dialogSku: false
    }
  },
  created() {
    this.skuClassList()
  },
  methods: {
    async skuClassList(className) {
      this.loading = true
      const { data } = await skuClassList({ ...this.page, ...className })
      this.loading = false
      this.list = data.currentPageRecords
      this.totalCount = Number(data.totalCount)
      this.totalPage = Number(data.totalPage)
    },
    // 1.查询
    Search() {
      this.skuClassList(this.formInline)
    },
    // 2.新增
    addForm() {
      this.dialogSku = true
    },
    // 3.分页
    changePage(val) {
      this.page.pageIndex = val
      this.skuClassList()
    },
    // 4.查看详情
    detailsFn(row) {
      this.addSkuForm = row
      this.dialogSku = true
    },
    // 5.商品类型弹出层
    closeDialog() {
      this.$refs.addSkuRef.resetFields()
      this.addSkuForm = {
        className: ''
      }
      this.dialogSku = false
    },
    openDialog() {},
    skuSubmit() {
      this.$refs.addSkuRef.validate(async(isOk) => {
        if (!isOk) return
        if (this.addSkuForm.classId) {
          await EditskuClass(this.addSkuForm)
        } else {
          await AddskuClass(this.addSkuForm)
        }

        this.skuClassList()
        this.dialogSku = false
      })
    }
  }
}
</script>

